package breakoutTest;

/* much code from http://zetcode.com/tutorials/javagamestutorial/basics/ 
 * and http://zetcode.com/tutorials/javagamestutorial/animation/
 */

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.Toolkit;
import javax.swing.JPanel;

public class Board extends JPanel implements Runnable {
	private Thread animator;
	private final int DELAY = 50;
	private Paddle paddle;
	private Brick brick;
	private Ball ball;
	private double w;
	private double h;
	private double counter = 0;
	private int brickcount = 0;
	
	public Board(){ 
        addKeyListener(new TAdapter());
        setFocusable(true);
		setBackground(Color.GREEN);
		setDoubleBuffered(true);
		paddle = new Paddle(40, 200);
		brick = new Brick(100, 200);
		ball = new Ball();
		ball.width = 20;
		ball.height = 20;
		ball.x = 50;
		ball.y = 50;
		ball.setXVelocity(1);
		ball.setYVelocity(1);
		brick.x = 500;
		brick.y = 400;
	}
	
	public void addNotify() {
		super.addNotify();
		animator = new Thread(this);
		animator.start();
	}
	public void paint(Graphics g){
		super.paint(g);
		Graphics2D g2 = (Graphics2D) g;
		Dimension size = getSize();
	    w = size.getWidth();	
	    h = size.getHeight();
//		RenderingHints rh =
//				new RenderingHints(RenderingHints.KEY_ANTIALIASING,
//	                               RenderingHints.VALUE_ANTIALIAS_ON);
//		rh.put(RenderingHints.KEY_RENDERING,
//	             RenderingHints.VALUE_RENDER_QUALITY);
//		g2.setRenderingHints(rh);
	    
	    g2.setStroke(new BasicStroke(1));
	    g2.setColor(Color.black);
	    g2.fill(ball);
	    g2.fill(paddle);
	    if(brick.get_life()>0)
	    	g2.fill(brick);
	    Toolkit.getDefaultToolkit().sync();
        g.dispose();
	}
	public void moveStuff(){
		
		ball.move();
		paddle.move();
		
	}
	public void checkCollisions(){
		double ballCenterx = ball.x + ball.width/2;
		double ballCentery = ball.y + ball.height/2;
		if(ball.x>=w||ball.x<=0){
			ball.invertXVelocity();
		}
		if(ball.y>=h||ball.y<=0){
			ball.invertYVelocity();
		}
		if(ball.intersects(paddle)){
			if(ballCenterx>=paddle.x&&ballCenterx<=(paddle.x+paddle.width)){
				ball.invertYVelocity();
			}
			else if(ballCentery>=paddle.y&&ballCentery<=(paddle.y+paddle.height)){
				ball.invertXVelocity();
			}
			else {
				ball.invertXVelocity();
				ball.invertYVelocity();
			}
		}
		if(brick.get_life()>0&&ball.intersects(brick)){
			if(ballCenterx>=brick.x&&ballCenterx<=(brick.x+brick.width)){
				ball.invertYVelocity();
			}
			else if(ballCentery>=brick.y&&ballCentery<=(brick.y+brick.height)){
				ball.invertXVelocity();
			}
			else {
				ball.invertXVelocity();
				ball.invertYVelocity();
			}
			brick.dec_life();
			
		}
	}
	private class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
            paddle.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            paddle.keyPressed(e);
        }
    }
	public void run(){
		long beforeTime, timeDiff, sleep;
		beforeTime = System.currentTimeMillis();
		while (true){
			checkCollisions();
			moveStuff();
			repaint();
			
			timeDiff = System.currentTimeMillis() - beforeTime;
			sleep = DELAY - timeDiff;
			
			if (sleep<0)
				sleep = 2;
			try {
				Thread.sleep(sleep);
			} catch (InterruptedException e) {
				System.out.println("interrupted");
			}
		}
	}
}
